package cbir;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;

public class Rss {

	private String rss;
	private String servletName;
	private String serverName;
	private String contextPath;
	
	public Rss(String servletName, String serverName, String contextPath) {
		this.rss = "";
		this.servletName = servletName;
		this.serverName = serverName;
		this.contextPath = contextPath;
	}
	
	
	public String getRss() {
		return rss;
	}

	@SuppressWarnings("deprecation")
	public void createRss(ArrayList<Img> imgs) {
		this.rss = this.rss.concat( "<?xml version='1.0' encoding='UTF-8' standalone='no'?><rss version='2.0' xmlns:cbir = 'http://sites.google.com/cbir-ppmm/' xmlns:atom='http://www.w3.org/2005/Atom'><channel><atom:link href='http://"+serverName+":"+contextPath+ "/" + servletName+"' rel='self' type='application/rss+xml' /><title>CBIR:"+servletName+"</title><link>http://sites.google.com/site/cbirppmm/</link><description>"+servletName+"</description>" );
		
		Iterator<Img> it = imgs.iterator();
		while(it.hasNext()){
			Img current = (Img) it.next();
			this.rss = this.rss.concat("<item><title>" + URLEncoder.encode(current.getName()).replaceAll("[+]", " ") + "</title><guid isPermaLink='false'>" + current.getImgURL() + "</guid><link>" + current.getImgURL() + "</link><description>" + URLEncoder.encode(current.getName()).replaceAll("[+]", " ") +  "</description><cbir:thumbURL>" + current.getThumbURL() + "</cbir:thumbURL><cbir:height>" + current.getHeight() + "</cbir:height><cbir:width>" + current.getWidth() + "</cbir:width><cbir:isRemote>"+ current.isRemote() +"</cbir:isRemote><cbir:globalRank>"+current.getGlobalRank()+"</cbir:globalRank><cbir:colorScore>"+current.getColorScore()+"</cbir:colorScore><cbir:textureScore>"+current.getTextureScore()+"</cbir:textureScore><cbir:similarity>"+current.getSimilarityScore()+"</cbir:similarity><cbir:originalURL>" + current.getOriginalURL() + "</cbir:originalURL><cbir:imageURI>" + current.getImageURI() +"</cbir:imageURI>");
			this.rss = this.rss.concat("<cbir:tags>");
			Iterator<String> it2 = current.getTags().iterator();
			while(it2.hasNext()){
				String currentTag = (String) it2.next();
				this.rss = this.rss.concat("<cbir:tag>"+currentTag+"</cbir:tag>");
			}
			this.rss = this.rss.concat("</cbir:tags></item>");
		}
		
		this.rss = rss.concat("</channel></rss>");
	}
	
	@SuppressWarnings("deprecation")
	public void createRssWithoutClosingIt(ArrayList<Img> imgs) throws UnsupportedEncodingException {
		this.rss = this.rss.concat( "<?xml version='1.0' encoding='UTF-8' standalone='no'?><rss version='2.0' xmlns:cbir = 'http://sites.google.com/cbir-ppmm/' xmlns:atom='http://www.w3.org/2005/Atom'><channel><atom:link href='http://" + serverName +":" + contextPath +"/" + servletName +"' rel='self' type='application/rss+xml' /><title>CBIR:"+servletName+"</title><link>http://sites.google.com/site/cbirppmm/</link><description>"+servletName+"</description>" );
		
		Iterator<Img> it = imgs.iterator();
		while(it.hasNext()){
			Img current = (Img) it.next();
			this.rss = this.rss.concat("<item><title>" + URLEncoder.encode(current.getName()).replaceAll("[+]", " ") + "</title><guid isPermaLink='false'>" + current.getImgURL() + "</guid><link>" + current.getImgURL() + "</link><description>" + URLEncoder.encode(current.getName()).replaceAll("[+]", " ")+  "</description><cbir:thumbURL>" + current.getThumbURL() + "</cbir:thumbURL><cbir:height>" + current.getHeight() + "</cbir:height><cbir:width>" + current.getWidth() + "</cbir:width><cbir:isRemote>"+ current.isRemote() +"</cbir:isRemote><cbir:globalRank>"+current.getGlobalRank()+"</cbir:globalRank><cbir:colorScore>"+current.getColorScore()+"</cbir:colorScore><cbir:textureScore>"+current.getTextureScore()+"</cbir:textureScore><cbir:similarity>"+current.getSimilarityScore()+"</cbir:similarity><cbir:originalURL>" + current.getOriginalURL() + "</cbir:originalURL><cbir:imageURI>" + current.getImageURI() +"</cbir:imageURI>");
			this.rss = this.rss.concat("<cbir:tags>");
			Iterator<String> it2 = current.getTags().iterator();
			while(it2.hasNext()){
				String currentTag = (String) it2.next();
				this.rss = this.rss.concat("<cbir:tag>"+currentTag+"</cbir:tag>");
			}
			this.rss = this.rss.concat("</cbir:tags></item>");
		}
	}

	public void setServletName(String servletName) {
		this.servletName = servletName;
	}

	public String getServletName() {
		return servletName;
	}

	public String getServerName() {
		return serverName;
	}

	public void setServerName(String serverName) {
		this.serverName = serverName;
	}

	public String getContextPath(){
		return contextPath;
	}
	
	public void setContextPath(String contextPath){
		this.contextPath = contextPath;
	}	
}